package com.bawei.mall.product.service.impl;

import com.bawei.mall.product.domain.CalcAmount;
import com.bawei.mall.product.domain.CusUserReceiveAddress;
import com.bawei.mall.product.domain.IntegrationConsumeSetting;
import com.bawei.mall.product.domain.MallProductOrderInfo;
import com.bawei.mall.product.domain.model.OrderModel;
import com.bawei.mall.product.domain.model.ShoppingCartModel;
import com.bawei.mall.product.domain.request.OrderRequest;
import com.bawei.mall.product.mapper.CusUserOrderInfoMapper;
import com.bawei.mall.product.mapper.MallProductShoppingCartMapper;
import com.bawei.mall.product.service.MallProductOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MallProductOrderInfoServiceImpl
 * @Description 描述
 * @Author ZiZheng.Li
 * @Date 2024/7/9 12:00
 */
@Service
public class MallProductOrderInfoServiceImpl implements MallProductOrderInfoService {
    @Autowired
    private CusUserOrderInfoMapper orderInfoMapper;
    @Autowired
    private MallProductShoppingCartMapper shoppingCartMapper;

    @Override
    public List<MallProductOrderInfo> list(MallProductOrderInfo mallProductInfo) {
        return orderInfoMapper.list(mallProductInfo);
    }

    @Override
    public void generateOrder(OrderRequest orderRequest) {
        List<Integer> integers = new ArrayList<>();
        List<ShoppingCartModel> shoppingCartModels = shoppingCartMapper.queryList(Long.valueOf(2));
        shoppingCartModels.forEach(shoppingCartModel -> {
            integers.add(shoppingCartModel.getId());
        });
        orderRequest.setCartIds(integers);
        orderRequest.setCouponId(0);
        orderRequest.setMemberReceiveAddressId(1);
        orderRequest.setPayType(1);
        orderRequest.setUseIntegration(0);

        orderInfoMapper.generateOrder(orderRequest);
    }

    @Override
    public OrderModel generateConfirmOrder(List<Integer> ids) {
        OrderModel orderModel = new OrderModel();
//        OrderModel orderModel = orderInfoMapper.generateConfirmOrder(ids);


        orderModel.setCartPromotionItemList(data().getCartPromotionItemList());
        orderModel.setCalcAmount(data().getCalcAmount());
        orderModel.setCouponHistoryDetailList(data().getCouponHistoryDetailList());
        orderModel.setMemberReceiveAddressList(data().getMemberReceiveAddressList());
        orderModel.setMemberIntegration(data().getMemberIntegration());
        orderModel.setIntegrationConsumeSetting(data().getIntegrationConsumeSetting());
        return orderModel;
    }

    public OrderModel data(){
        OrderModel orderModel = new OrderModel();
        BigDecimal payAmount = BigDecimal.ZERO;
        BigDecimal freightAmount = BigDecimal.ZERO;
        BigDecimal promotionAmount = new BigDecimal(200.00);


        List<ShoppingCartModel> shoppingCartModels = shoppingCartMapper.queryList(Long.valueOf(2));
        for (ShoppingCartModel shoppingCartModel : shoppingCartModels) {
            shoppingCartModel.setProductAttr("[{\"key\":\"颜色\",\"value\":\"黑色\"},{\"key\":\"容量\",\"value\":\"128G\"}]");
            payAmount=payAmount.add(shoppingCartModel.getPrice());
        }

        CalcAmount calcAmount = new CalcAmount();
        calcAmount.setFreightAmount(freightAmount);
        calcAmount.setPromotionAmount(promotionAmount);
        calcAmount.setPayAmount(payAmount.subtract(calcAmount.getPromotionAmount()));
        calcAmount.setTotalAmount(payAmount);

        List<String> couponHistoryDetailList = new ArrayList<>();

        IntegrationConsumeSetting integrationConsumeSetting = new IntegrationConsumeSetting();
        integrationConsumeSetting.setCouponStatus(1);
        integrationConsumeSetting.setDeductionPerAmount(100);
        integrationConsumeSetting.setId(1);
        integrationConsumeSetting.setMaxPercentPerOrder(50);
        integrationConsumeSetting.setUseUnit(100);

        ArrayList<CusUserReceiveAddress> cusUserReceiveAddresses = new ArrayList<>();

        CusUserReceiveAddress address1 = new CusUserReceiveAddress();
        address1.setId(1);
        address1.setMemberId(1);
        address1.setDeleteStatus("1");
        address1.setPostCode("510000");
        address1.setName("张三");
        address1.setPhoneNumber("123456789");
        address1.setProvince("广东省");
        address1.setCity("广州市");
        address1.setRegion("海珠区");
        address1.setDetailAddress("海珠区海珠路");
        cusUserReceiveAddresses.add(address1);
        CusUserReceiveAddress address2 = new CusUserReceiveAddress();
        address2.setId(2);
        address2.setMemberId(2);
        address2.setDeleteStatus("0");
        address2.setPostCode("510000");
        address2.setName("李四");
        address2.setPhoneNumber("12345678911");
        address2.setProvince("广东省");
        address2.setCity("广州市");
        address2.setRegion("海珠区");
        address2.setDetailAddress("海珠区海珠路11");
        cusUserReceiveAddresses.add(address2);

        orderModel.setCalcAmount(calcAmount);
        orderModel.setCouponHistoryDetailList(couponHistoryDetailList);
        orderModel.setMemberIntegration(5000);
        orderModel.setCartPromotionItemList(shoppingCartModels);
        orderModel.setIntegrationConsumeSetting(integrationConsumeSetting);
        orderModel.setMemberReceiveAddressList(cusUserReceiveAddresses);
        return orderModel;
    }
}
